import createApp from '../src/main'

export default function (ctx) {
  return new Promise((resolve, reject) => {
    const { app, router, store } = createApp()
    router.push(ctx.url)
    router.onReady(() => {
      const matched = router.getMatchedComponents()
      console.log('match', matched)
      if (matched.length === 0) {
        return reject({ code: 404 })
      }
      Promise.all(matched.map(comp => {
        if (comp.asyncData) {
          return comp.asyncData(store)
        }
      }))
        .then(() => {
          console.log()
          ctx.state = store.state
          resolve(app)
        }).catch(reject)
    }, reject)
  })
}